﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using My.GenCode.Entities;
using My.GenCode.Interface;

namespace My.GenCode.Templates
{
    public class GenSqlMap : CodeTemplate
    {
        #region 覆盖父类的方法

        public GenSqlMap(string tpl, EntityInfo info, string desc) : base(tpl, info, desc) { }

        protected override string ProcTemplate()
        {
            _tpl = LoopProperties(_tpl, "<<ResultMapFiled>>", this.GenResultMapField);

            _tpl = _tpl.Replace("<FileName>", _nh.SqlMapFileName);                   // 处理<FileName>标签
            _tpl = _tpl.Replace("<<EntitiesName>>", _nh.EntitiesName);               // 处理<<EntitiesName>>标签
            _tpl = _tpl.Replace("<<NameSpace>>", _nh.NameSpace);                     // 处理<<NameSpace>>标签
            _tpl = _tpl.Replace("<<EntityName>>", _nh.EntityName);                   // 处理<<EntityName>>标签
            _tpl = _tpl.Replace("<<EntityClassName>>", _nh.EntityClassName);         // 处理<<ClassName>>标签

            _tpl = _tpl.Replace("<<CreateMethodName>>", _nh.CreateMethodName);
            _tpl = _tpl.Replace("<<UpdateMethodName>>", _nh.UpdateMethodName);
            _tpl = _tpl.Replace("<<DeleteMethodName>>", _nh.DeleteMethodName);
            _tpl = _tpl.Replace("<<ReadDetailMethodName>>", _nh.ReadDetailMethodName);
            _tpl = _tpl.Replace("<<ReadListMethodName>>", _nh.ReadListMethodName);
            _tpl = _tpl.Replace("<<ReadCountMethodName>>", _nh.ReadCountMethodName);

            _tpl = _tpl.Replace("<<PropertiesNames>>", this.GenPropertiesNames());
            _tpl = _tpl.Replace("<<UpdateNameValuePairs>>", this.GenUpdateNameValuePairs());
            _tpl = _tpl.Replace("<<InsertValues>>", this.InsertValues());

            return _tpl;
        }

        protected override string OutputFilename()
        {
            return @"Website\_SqlMap\" + _nh.SqlMapFileName;
        }

        #endregion

        #region 处理循环标签方法

        /// <summary>
        /// [loop<<ResultMapFiled>>]标签的处理函数
        /// </summary>
        /// <param name="tpl"></param>
        /// <returns></returns>
        private string GenResultMapField(string tpl,PropertyInfo p)
        {
            tpl = tpl.Replace("<<ClassFieldName>>", p.Name);
            tpl = tpl.Replace("<<DbFieldName>>", p.Name);
            return tpl;
        }

        /// <summary>
        /// 属性名列表，用户增加，查询
        /// </summary>
        /// <returns></returns>
        private string GenPropertiesNames()
        {
            List<string> list = new List<string>();
            foreach( PropertyInfo info in _info.Properties )
            {
                list.Add(info.Name);
            }
            return string.Join("\r\n               ,", list.ToArray());
        }

        /// <summary>
        /// 更新名值对
        /// </summary>
        /// <returns></returns>
        private string GenUpdateNameValuePairs()
        {
            List<string> list = new List<string>();
            foreach (PropertyInfo info in _info.Properties)
            {
                list.Add(string.Format("{0}=#{0}#",info.Name));
            }
            return string.Join("\r\n               ,", list.ToArray());
        }

        private string InsertValues()
        {
            List<string> list = new List<string>();
            for (int i = 0, len = _info.Properties.Count; i < len; i++)
            {
                list.Add("?");
            }
            return string.Join(",", list.ToArray());
        }
        #endregion
    }
}